package com.defangit.production.orm;

import com.defangit.production.bll.StoreBll;
import com.defangit.production.domain.Store;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;

public class StoreService extends CommonService<Store> implements StoreBll {

    @Override
    public Store addStore(Store store) {
        if (store==null) throw ParameterNotValidException.nullParameter("仓库实体异常");
        add(store);
        commitSession();
        return get(store.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("仓库实体"));
    }

    @Override
    public Store updateStore(Store store) {
        if (store==null) throw ParameterNotValidException.nullParameter("仓库实体异常");
        update(store);
        commitSession();
        return get(store.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("仓库实体异常"));
    }

    @Override
    public StoreFinder findAll() {
        return new StoreFinderImp();
    }

    public class StoreFinderImp extends CommonFinder<Store> implements StoreFinder{

        private DetachedCriteria storeCriteria;

        public StoreFinderImp(){
            this.storeCriteria = detachedAll();
        }
        @Override
        public Criteria getCriteria() {
            return this.storeCriteria.getExecutableCriteria(getSession());
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return this.storeCriteria;
        }
    }
}
